package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_stats;

import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiT_statsService;
import cn.com.api.service.StartOnLoadService;
import cn.com.backstage.controller.base.T_statsBaseController;

import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_statsController extends T_statsBaseController {

	private static Logger logger = Logger.getLogger(T_statsController.class);

	@Autowired
	private ApiT_statsService apiT_statsService;
	@Autowired
	private ApiShopsService shopsService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "stats-list")
	public ModelAndView stats_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/stats_list.jsp");
		String shopname=request.getParameter("shopname");
		String balance=request.getParameter("balance");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");
		String cityid=request.getParameter("cityid");
		String dis=request.getParameter("discount");
		DecimalFormat df = new DecimalFormat("######0.00");
		String d=dis==null?"":dis==""?"":df.format(Double.parseDouble(dis)/100);
		List<Map<String,Object>> stats=new ArrayList<Map<String,Object>>();
		stats=apiT_statsService.shopAccount(shopname==null?"":shopname, balance==null?"":balance, stime==null?"":stime, etime==null?"":etime, dis==null?"":dis==""?"":df.format(Double.parseDouble(dis)/100), cityid==null?"":cityid);
		for(Map<String,Object> m:stats){
			Map<String,Object> shop=new HashMap<String,Object>();
			shop=shopsService.findShopByShopId(m.get("shopid")+"");
			Double discount=Double.parseDouble(m.get("discount")+"");//折扣
			Double cashmoney=Double.parseDouble(shop.get("cash_money")+"");
			Double final_pay_money=Double.parseDouble(m.get("final_pay_money")+"");
			m.put("orders_money", df.format(final_pay_money/discount));//订单总额去除配送费
			m.put("fg", final_pay_money);//付给商家金额
			m.put("discount", df.format((1-discount)*100));
			if("周结算".equals(m.get("js"))||"月结算".equals(m.get("js"))){
				m.put("wjs", df.format(cashmoney/discount));
				m.put("yjs", cashmoney);
				m.put("pre_payment", "---");
			}else{
				m.put("wjs", "---");
				m.put("yjs", "---");
				m.put("pre_payment", shop.get("pre_payment"));
			}
		}
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("stats",stats);
		mv.addObject("shopname",shopname);
		mv.addObject("balance",balance);
		mv.addObject("stime",stime);
		mv.addObject("etime",etime);
		mv.addObject("cityid",cityid);
		mv.addObject("discount",dis);
		return mv;
	}
	
	/**
	 * 查看详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "shopOrderDel")
	public ModelAndView shopOrderDel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shop_order_del_list.jsp");
		String shopid=request.getParameter("shopid");
		String rid=request.getParameter("rid");
		String time=request.getParameter("time");
		String stime=request.getParameter("stime");
		String etime=request.getParameter("etime");
		String par="";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(etime!=null&&!"".equals(etime)){
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(etime));
			cal.add(Calendar.DATE, 1);
			par=sdf.format(cal.getTime());
		}else{
			par=etime;
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map=shopsService.searShopOrderDel(shopid,rid,time,stime,par);
		mv.addObject("datas",map.get("data"));
		mv.addObject("money",map.get("money"));
		mv.addObject("rid",rid);
		mv.addObject("time",time);
		mv.addObject("stime",stime);
		mv.addObject("etime",etime);
		return mv;
	}

}
